// pages/Index/shopActive/coupon/index.js
const controllers = require('../../../controllers/controllerIndex').controller
const controller=require("../../../controllers/Controller").controller

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponlist: [],
    pendimg: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/index/img/coupon_bg_05@2x.png',
    overimg: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/index/img/coupon_bg_07@2x.png',
    noimg: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/index/img/coupon_bg_09@2x.png',
    pageNumber: 1,
    pageSize: 10,
    isEnd: false,
  },

  //删除优惠券
  delCoupon(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.couponId=this.data.couponlist[index].id
    let that=this

    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success (res) {
        if (res.confirm) {
          controller.delCoupon(value).then(res=>{
            wx.showToast({
              title: '删除成功',
            })
            that.setData({
              couponlist: [],
              isEnd: false,
              pageNumber: 1,
            })
            that.couponlist()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //跳转到详情页面
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/Index/couponActivity/couponDetail/index?id=' + e.currentTarget.id + '&ispush=' + e.currentTarget.dataset.ispush,
    })
  },
  //获取列表
  couponlist() {
    //获取优惠券列表
    controllers.getCouponList({
      shopId: wx.getStorageSync('shopId'),
      pageNumber: this.data.pageNumber,
      pageSize: this.data.pageSize,
      payType: 0,
      isVip:0
    }).then(res => {
      if (res.data.list.length != 0) {
        this.setData({
          couponlist: this.data.couponlist.concat(res.data.list)
        })
      }
      if (res.data.lastPage) {
        this.setData({
          isEnd: true
        })
      }
      var all = this.data.couponlist
      all.forEach(function (item, index) {
        let data = new Date().getTime();
        let start = new Date(item.startTime.replace(/-/g, '/')).getTime()
        let end = new Date(item.endTime.replace(/-/g, '/')).getTime()
        console.log(start + 'ccc' + end);
        if (data < start) {
          all[index].state = 0
        } else if (data > start && data < end) {
          all[index].state = 1
        } else {
          all[index].state = 2
        }
        console.log(all[index].state);

      })
      this.setData({
        couponlist: all
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      couponlist: [],
      isEnd: false,
      pageNumber: 1,
    })
    this.couponlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      });
      this.couponlist()
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})